<?php

namespace App\HttpController;

use App\Service\PropertyService;
use App\Service\SensitiveService;
use App\Utility\Verification;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="物业服务")
 */
class Property extends BaseController
{
    /**
     * @Api(name="访客通行", path="/property/visit", version="1.0.0")
     * @ApiDescription(value="访客通行")
     * @Method(allow={POST})
     * @Param(name="house", description="受访楼号", required="受访楼号必填", from={JSON})
     * @Param(name="time", description="受访时间", required="受访时间必填", from={JSON})
     * @Param(name="name", description="访客姓名", required="访客姓名必填", from={JSON})
     * @Param(name="tel", description="访问电话", required="访问电话必填", from={JSON})
     * @Param(name="person", description="同行人数", required="同行人数必填", from={JSON})
     * @Param(name="is_car", description="是否开车", required="是否开车必填", from={JSON})
     * @Param(name="car", description="车牌号", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function visit($house, $time, $name, $tel, $person, $is_car, $car): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $checkPhone = Verification::getInstance()->is_phone($tel);
        if (!$checkPhone) {
            return $this->writeJsonError('手机号输入错误');
        }
        $res = PropertyService::getInstance()->visit($this->uid, $house, $time, $name, $tel, $person, $is_car, $car);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="报事报修", path="/property/repair", version="1.0.0")
     * @ApiDescription(value="报事报修")
     * @Method(allow={POST})
     * @Param(name="house", description="楼号", required="楼号必填", from={JSON})
     * @Param(name="time", description="时间", required="时间必填", from={JSON})
     * @Param(name="name", description="姓名", required="姓名必填", from={JSON})
     * @Param(name="tel", description="电话", required="电话必填", from={JSON})
     * @Param(name="name1", description="临时姓名", from={JSON})
     * @Param(name="tel1", description="临时电话", from={JSON})
     * @Param(name="type", description="类型", from={JSON})
     * @Param(name="content", description="维修内容", required="维修内容必填", from={JSON})
     * @Param(name="images", description="上传图片", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function repair($house, $time, $name, $tel, $type, $name1, $tel1, $content, $images): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $checkPhone = Verification::getInstance()->is_phone($tel);
        if (!$checkPhone) {
            return $this->writeJsonError('手机号输入错误');
        }
        if (!empty($tel1)) {
            $checkPhone = Verification::getInstance()->is_phone($tel1);
            if (!$checkPhone) {
                return $this->writeJsonError('手机号2输入错误');
            }
        }

        $res = PropertyService::getInstance()->repair($this->uid, $house, $time, $name, $tel, $type, $name1, $tel1, $content, $images);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的访客", path="/property/getVisit", version="1.0.0")
     * @ApiDescription(value="获取我的访客")
     * @Method(allow={POST})
     * @Param(name="status", defaultValue="99", description="状态：0.待处理，1.已处理，-1.未处理", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getVisit($status): bool
    {
        $res = PropertyService::getInstance()->getVisit($this->uid, $status);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的报修", path="/property/getRepair", version="1.0.0")
     * @ApiDescription(value="获取我的报修")
     * @Method(allow={POST})
     * @Param(name="status", defaultValue="99", description="状态：0.待处理，1.已处理，2.处理中，-1.未处理", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getRepair($status): bool
    {
        $res = PropertyService::getInstance()->getRepair($this->uid, $status);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的访客详情", path="/property/getVisitDetail", version="1.0.0")
     * @ApiDescription(value="获取我的访客详情")
     * @Method(allow={POST})
     * @Param(name="id", description="id", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getVisitDetail($id): bool
    {
        $res = PropertyService::getInstance()->getVisitDetail($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的报修详情", path="/property/getRepairDetail", version="1.0.0")
     * @ApiDescription(value="获取我的报修详情")
     * @Method(allow={POST})
     * @Param(name="id", description="id", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getRepairDetail($id): bool
    {
        $res = PropertyService::getInstance()->getRepairDetail($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取巡查记录", path="/property/getPatrolList", version="1.0.0")
     * @ApiDescription(value="获取巡查记录")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getPatrolList(): bool
    {
        $res = PropertyService::getInstance()->getPatrolList();
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="报事报修付款", path="/property/confirmRepair", version="1.0.0")
     * @ApiDescription(value="报事报修付款")
     * @Method(allow={POST})
     * @Param(name="gid", description="报修事物id", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function confirmRepair($gid): bool
    {
        $ips = $this->request()->getHeaders()['x-real-ip'] ?? [];
        $this->ip = $ips[0] ?? '127.0.0.1';
        $res = PropertyService::getInstance()->confirmRepair($this->uid, $gid, $this->ip);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取代缴物业费", path="/property/getPropertyFee", version="1.0.0")
     * @ApiDescription(value="获取代缴物业费")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getPropertyFee(): bool
    {
        $res = PropertyService::getInstance()->getPropertyFee($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="物业费订单生成", path="/property/createOrder", version="1.0.0")
     * @ApiDescription(value="物业费订单生成")
     * @Method(allow={POST})
     * @Param(name="hid", description="楼号id", required="楼号id必填", from={JSON})
     * @Param(name="param", description="数据", required="缴费项必填", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function createOrder($hid, $param): bool
    {
        $ips = $this->request()->getHeaders()['x-real-ip'] ?? [];
        $this->ip = $ips[0] ?? '127.0.0.1';
        $res = PropertyService::getInstance()->createOrder($this->uid, $hid, $param, $this->ip);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取待支付订单", path="/property/getOrder", version="1.0.0")
     * @ApiDescription(value="获取待支付订单")
     * @Method(allow={POST})
     * @Param(name="id", description="订单id", required="订单id必填", from={JSON})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getOrder($id): bool
    {
        $res = PropertyService::getInstance()->getOrder($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的缴费订单", path="/property/getMyOrders", version="1.0.0")
     * @ApiDescription(value="获取我的缴费订单")
     * @Method(allow={POST})
     * @Param(name="type", description="类型", defaultValue="99", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getMyOrders($type): bool
    {
        $res = PropertyService::getInstance()->getMyOrders($this->uid, $type);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    public function getMyFee(): bool
    {
        $res = PropertyService::getInstance()->getMyFee($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="删除我的访客", path="/property/delVisit", version="1.0.0")
     * @ApiDescription(value="删除我的访客")
     * @Method(allow={POST})
     * @Param(name="id", description="id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function delVisit($id): bool
    {
        $res = PropertyService::getInstance()->delVisit($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="删除我的报修", path="/property/delRepair", version="1.0.0")
     * @ApiDescription(value="删除我的报修")
     * @Method(allow={POST})
     * @Param(name="id", description="id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function delRepair($id): bool
    {
        $res = PropertyService::getInstance()->delRepair($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="评论管家", path="/property/keeperComment", version="1.0.0")
     * @ApiDescription(value="评论管家")
     * @Method(allow={POST})
     * @Param(name="sid", description="客服id", required="", notEmpty="", from={JSON})
     * @Param(name="star", description="星级", required="", notEmpty="", from={JSON})
     * @Param(name="content", description="评论内容", required="", notEmpty="", from={JSON})
     * @Param(name="is_anonymous", description="是否匿名：0，否，1，是", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception|Throwable
     */
    public function keeperComment($sid, $star, $content, $is_anonymous): bool
    {
        $res = PropertyService::getInstance()->keeperComment($this->uid, $sid, $star, $content, $is_anonymous);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}